package Interface;


import Interface.InterfaceJPanel;
import chessboard.ChessBoard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author agnieszka
 */
public class MainInterface {

        static JFrame okno;
        static InterfaceJPanel window;

        public static ChessBoard exchange;

        static JMenuBar menuBar;
        static JMenu menu;
        static JMenuItem menu_newGame;
        static JMenuItem loadGameItem;
        static JMenuItem connectionItem;
        static JMenuItem saveGameItem;
        static JMenuItem unconItem;
        static JMenuItem conItem;
        static JMenuItem player1;
        static JMenuItem player2;

        static boolean change=true, isRuning = false, first=true;

        static ButtonGroup p1group;
        static ButtonGroup p2group;


    public static void main(String [] args) {                                   //create the main window
       
            okno = new JFrame("POPAPLORE");
            window = new InterfaceJPanel ();
            try {
                exchange = new ChessBoard(window.getPanel());
            } catch (Exception ex) {

            }

            okno.setSize(840,700);
            okno.add(window);
            okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            okno.setResizable(false);
            okno.setVisible(true);


            menuBar = new JMenuBar();
            menu = new JMenu("Game");
            menu_newGame = new JMenuItem("New Game");
            menu_newGame.setMnemonic(KeyEvent.VK_F2);
            menu_newGame.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if(isRuning){
                        isRuning=!isRuning;
                        menu_newGame.setText("New Game");

                        window.actionStop(e);
                        player1.setEnabled(true);
                        player2.setEnabled(true);
                        loadGameItem.setEnabled(true);
                        saveGameItem.setEnabled(false);
                        //connectionItem.setEnabled(true);
                       window.thinkingBar.setVisible(false);
                       window.thinkingLabel.setVisible(false);


                    }else{
                        isRuning=!isRuning;
                        menu_newGame.setText("Stop");
                        window.actionNewGame(e);
                        window.actionRun(e);

                        player1.setEnabled(false);
                        player2.setEnabled(false);
                       loadGameItem.setEnabled(false);
                       saveGameItem.setEnabled(true);
                       //connectionItem.setEnabled(false);

                       window.thinkingBar.setVisible(false);
                       window.thinkingLabel.setVisible(false);

                    }
                }
            });


            player1 = new JMenu("player1");
            player2 = new JMenu("player2");

            //creating radiobutton group for player 1
            p1group = new ButtonGroup();

            JRadioButtonMenuItem player1Human = new JRadioButtonMenuItem("Human");
            player1Human.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    window.rb1UserActionPerformed1(e);
                }
            });
    //        player1Human.doClick();

            JRadioButtonMenuItem player1Online = new JRadioButtonMenuItem("On-line user");
            player1Online.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    window.rb1OnlineActionPerformed1(e);
                    window.p1RBOnlineSetting(e);

                }
            });

            JRadioButtonMenuItem player1Neural = new JRadioButtonMenuItem("Neural Engine");
            player1Neural.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    window.rb1NeuralActionPerformed1(e);
                }
            });



            p1group.add(player1Human);
            p1group.add(player1Online);
            p1group.add(player1Neural);


            player1.add(player1Human);
            player1.add(player1Online);
            player1.add(player1Neural);


            //player 2 menu ####################3
            //creating radiobutton group for player 1
            p2group = new ButtonGroup();
            JRadioButtonMenuItem player2Human = new JRadioButtonMenuItem("Human");
            player2Human.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    window.rb2UserActionPerformed1(e);
                }
            });
    //        player2Human.doClick();

            JRadioButtonMenuItem player2Online = new JRadioButtonMenuItem("On-line user");
            player2Online.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    window.rb2OnlineActionPerformed1(e);
                    window.p2RBOnlineSetting(e);
                }
            });
            JRadioButtonMenuItem player2Neural = new JRadioButtonMenuItem("Neural Engine");
            player2Neural.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    window.rb2NeuralActionPerformed1(e);
                }
            });


            p2group.add(player2Human);
            p2group.add(player2Online);
            p2group.add(player2Neural);

            player2.add(player2Human);
            player2.add(player2Online);
            player2.add(player2Neural);




            saveGameItem = new JMenuItem("Save Game");
            saveGameItem.setEnabled(false);
            saveGameItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    window.actionSaveGame(e);
                }
            });

            loadGameItem = new JMenuItem("Load Game");
            loadGameItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    window.actionLoadGame(e);
                    window.actionRun(e);
                    isRuning = true;

                    player1.setEnabled(false);
                    player2.setEnabled(false);
                    menu_newGame.setText("Stop");
                    loadGameItem.setEnabled(false);
                    saveGameItem.setEnabled(true);
                }
            });


//            connectionItem = new JMenuItem("Connection with Robots");
//            connectionItem.addActionListener(new ActionListener() {
//
//                public void actionPerformed(ActionEvent e) {
//
//
//                }
//            });
//

            menu.add(menu_newGame);
            menu.add(loadGameItem);
            menu.add(saveGameItem);
//            menu.add(connectionItem);
            menu.addSeparator();

            menu.add(player1);
            menu.add(player2);


            //adding help menu
            JMenu helpMenu = new JMenu("Help");
            JMenuItem helpItem = new JMenuItem("Instructions");
            helpItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    window.actionHelp(e);
                }
            });
            helpMenu.add(helpItem);


            JMenu connectedMenu = new JMenu("Robot connection");
            conItem = new JMenuItem("Connected");
            unconItem = new JMenuItem("Unconnected");
            conItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    unconItem.setEnabled(true);
                    conItem.setEnabled(false);
                    exchange.restoreConnectionWithRobots();
                }
            });

            unconItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    unconItem.setEnabled(false);
                    conItem.setEnabled(true);
                    exchange.closeConnectionWithRobots();
                }
            });
            connectedMenu.add(conItem);
            connectedMenu.add(unconItem);


            JMenuItem trainItem = new JMenuItem("Train network");
            trainItem.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    window.actionTrain(e);
                }
            });

            if(first){
             unconItem.setEnabled(false);
             conItem.setEnabled(true);
             first=false;
            }

            menuBar.add(menu);
            menuBar.add(helpMenu);
            menuBar.add(connectedMenu);
            okno.setJMenuBar(menuBar);
            player1Human.doClick();
            player2Human.doClick();

            okno.invalidate();
            okno.repaint();
            menuBar.repaint();


     }

}

